home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 033a / aed244a.zip / FINDWORD.ASM < prev    next >
Assembly Source File  |  1991-02-16  |  5KB  |  135 lines

  1. comment @
  2.  
  3.  *  FINDWORD
  4.  *----------------------------------------------------------------------------
  5.  *
  6.  *  Routine to find the next (direction = 1)  or previous (direction = 0)
  7.  *  word position in a QuickBASIC string
  8.  *
  9.  *  Author: Tom Collins
  10.  *          09-20-90
  11.  *
  12.  *  Typical Calling Sequence:
  13.  *
  14.  *     WordPos% = 1
  15.  *     A$ = "This is a test"
  16.  *     CALL FindWord(A$ ,1 ,WordPos%)
  17.  *
  18.  *  Assemble with: TASM upcase.asm [/Zi]   or,
  19.  *                 MASM upcase.asm [/Zi]
  20.  *  (Zi = Debug info for CV)
  21.  *
  22.     @
  23.  
  24.        .MODEL MEDIUM, BASIC
  25.  
  26.        .CODE
  27.  
  28.        PUBLIC  FINDWORD
  29.  
  30. FINDWORD  proc USES es si di, STRDES:ptr, Direction:ptr, Z:ptr
  31.  
  32. ; The registers are used as follows:
  33. ;
  34. ;    AH    = Direction flag
  35. ;    BL    = Z value
  36. ;    BH    = New return value
  37. ;    CL    = String length
  38. ;    CH    = Current string index in main loop
  39. ;    DS:SI = Current string index + 1 address
  40.  
  41.        push     ds                 ; Save DS
  42.        pop      es                 ; Set ES = DS
  43.  
  44.        mov      di, Direction      ; DS:DI -> Direction indicator
  45.        mov      ah, [di]           ; AH = Direction
  46.  
  47.        mov      di, Z              ; DS:DI -> Z value
  48.        mov      bl, [di]           ; BL = Where we're at now (Z value)
  49.  
  50.        mov      di, STRDES         ; DS:DI -> String descriptor
  51.        mov      cl, [di]           ; Get string length in CL ( < 256, right?? )
  52.        mov      si, [di+2]         ; DS:SI -> String
  53.  
  54.        mov      ch, 1              ; CH = String index counter
  55.  
  56. ; Test for invalid conditions
  57.  
  58.        mov      bh, bl             ; Assume no change in Z value
  59.        and      cl, cl             ; Test string length
  60.        jz       FW100              ; Null string.  Return
  61.  
  62.        cmp      cl, bl             ; See if we're starting past string end
  63.        jge      FW00               ; No
  64.        and      ah, ah             ; Yes. Test the direction.
  65.        jnz      FW100              ; Forward.  Return
  66. ;      mov      bh, cl             ; Backward.  Set return value to str len + 1
  67. ;      inc      bh                 ;
  68. ;      jmp      FW100              ; Return
  69.  
  70. ; Initialize the default return value based on the direction
  71.  
  72. FW00:  mov      bh, cl             ; Assume string length + 1
  73.        inc      bh                 ;
  74.        and      ah, ah             ; Set the flags
  75.        jnz      FW01               ; Backwards
  76.        mov      bh, 1              ; Backwards.  Assume length of 1
  77.  
  78. ; Main loop.
  79.  
  80. FW01:  lodsb                       ; Get a character from string into AL
  81.        cmp      al, ' '            ; Is it a space?
  82.        je       FW02               ; Yes
  83.        cmp      al, 0FAh           ; Is is a soft space?
  84.        jne      FW10               ; No
  85.  
  86. ; Found a space or soft space.  See what the next character is.
  87.  
  88. FW02:  mov      al, [si]           ; Get next character
  89.        cmp      al, ' '            ; Is it a space?
  90.        je       FW10               ; Yes. Continue loop
  91.        cmp      al, 0FAh           ; Is is a soft space?
  92.        je       FW10               ; Yes
  93.  
  94. ; Found a word position.  See which direction we're going in.
  95.  
  96.        and      ah, ah             ;
  97.        jz       FW04               ; Backwards
  98.  
  99. ; Forward direction.  If index > Z, set return value and exit.  Otherwise,
  100. ; keep looking.
  101.  
  102. FW03:  cmp      ch, bl             ; See if index > Z
  103.        jl       FW10               ; No. Keep looking.
  104.        inc      ch                 ; Yes. Save current index + 1 in return loc
  105.        mov      bh, ch             ;
  106.        jmp      short FW100        ; Return
  107.  
  108. ; Reverse direction.  If we're >= Z, return.  Otherwise set new value
  109. ; and look for the next word position.
  110.  
  111. FW04:  push     cx                 ; Save CX temporarily
  112.        inc      ch                 ; Add one to CH for comparison
  113.        cmp      ch, bl             ; See if index < Z
  114.        pop      cx                 ; Restore CX
  115.        jge      FW100              ; Yes.  Return
  116.        mov      bh, ch             ; No. Save current index + 1 in return loc
  117.        inc      bh                 ;
  118.        jmp      short FW10         ; Continue loop
  119.  
  120. ; Continue loop while not at the end of the string
  121.  
  122. FW10:  inc      ch                 ; Increment string index
  123.        cmp      cl, ch             ; Test for equal to string length
  124.        jge      FW01               ; Not equal.  Continue loop.
  125.  
  126. ; Return to the calling program
  127.  
  128. FW100: mov      di, Z              ; DS:DI -> Z
  129.        mov      [di], bh           ; Load return value
  130.        ret                         ; Return
  131.  
  132. FINDWORD endp
  133.  
  134.        end
  135.